﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;
using MixScript.BLL.FI;
namespace MixScriptWeb.Accounting
{
    public partial class fSearchCOA :cCommonWeb // System.Web.UI.Page
    {
        private string errMsg = "";
        private static cCOA oCoa;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                {
                    hfForm.Value= Request.QueryString["sform"];
                    hfContrl.Value = Request.QueryString["scontrol"];
                    hfLbDesc.Value = Request.QueryString["slbl"];
                    hflbParent.Value=Request.QueryString["spid"];
                    hfctrl.Value = Request.QueryString["hf"];
                    oCoa = new cCOA();
                    PopulateTree();
                }
                
            }
            catch (Exception ex)
            {
                lblmsg.Text = ex.Message;
            }
        }

        public void PopulateTree()
        {
             DataTable dt = new DataTable();
            Hashtable ht = new Hashtable();
            oCoa.getCoa(ref dt, 0, "", "", 0);
            //TreeNode tn= new TreeNode("ndcoa");

            //tvCOA.Nodes.Add(tn);

            errMsg = "";
            foreach (DataRow r in dt.Rows)
            {
                int nodeLvl = int.Parse(r["idcoa"].ToString());
                string nodeParent = r["Parentid"].ToString();
                string nodeName = nodeLvl.ToString() + "-" + r["coaName"].ToString();
                TreeNode tNode = new TreeNode(nodeName);

                
                ht.Add(nodeLvl.ToString(), tNode);


                if (nodeParent.Trim() == string.Empty)
                {
                    tvCOA.Nodes.Add(tNode);
                }
                else
                {

                    if (ht.ContainsKey(nodeParent))
                    {
                        TreeNode tn = new TreeNode();
                        tn = (TreeNode)ht[nodeParent];
                        tn.ChildNodes.Add(tNode);
                    }
                    else
                    {
                        errMsg += nodeParent + " ";
                    }

                }


                //if (tvCOA.Nodes.Count == 0)
                //    tvCOA.Nodes.Add(tNode);
                //else
                //{
                //    nodeLvl--;
                //    ((TreeNode)ht[nodeLvl.ToString() + nodeParent]).ChildNodes.Add(tNode);

                //}
            }
            ht.Clear();
        }

        protected void tvCOA_SelectedNodeChanged(object sender, EventArgs e)
        {
            try
            {
                TreeView tv = (TreeView)sender;
                TreeNode tn = tv.SelectedNode; // (TreeNode)sender;
                //string[] sname = tn.Text.Split('-');
                int ix = tn.Text.IndexOf('-');
                string pid = tn.Text.Substring(0, ix).Trim();
                string sname = tn.Text.Substring(ix + 1, tn.Text.Length - (ix + 1)).Trim();

           //     string sScrpt1 = string.Format("send2Child('{0}', '{1}', '{2}');", hdForm.Value, hdControl.Value, rw.Cells[1].Text);
                //string sScrpt = string.Format("send2Child('{0}', '{1}', '{2}');", hfForm.Value, hfCntrl1.Value, LONG_DSC);
                string sScrpt = "javascript:window.opener.document.forms['" + hfForm.Value + "'].elements['" + hfContrl.Value + "'].value = '" + pid + "';";
                //sScrpt += "javascript:window.opener.document.getElementById('"+ hfctrl.Value + "').value = '" + pid + "';";
                // sScrpt += "window.opener.document.forms['" + hdForm.Value + "'].elements['" + hdControl.Value + "'].value = '" +"ssss" + "'";
                sScrpt += "javascript:window.opener.document.getElementById('" + hfLbDesc.Value + "').innerHTML= '" + sname + "';";
               // sScrpt += "javascript:window.opener.document.getElementById('" + hflbParent.Value + "').innerHTML= '" + pid + "';";
                sScrpt += "javscript:window.close();";

                ClientScriptManager cs = this.ClientScript;
                if (!cs.IsClientScriptBlockRegistered(this.GetType(), "kk"))
                {
                    cs.RegisterClientScriptBlock(this.GetType(), "kk", sScrpt, true);
                }                    
            }
            catch (Exception ex)
            {

                lblmsg.Text = ex.Message;
            }
        }
    }
}